# -*- coding: utf-8 -*-
from PIL import Image,ImageEnhance,ImageTk
from tkinter import ttk
import numpy  as np
import rgb2gray
import lap
import gsph
import rgb2hsi
import fuliye
import fenceng
from skimage import exposure
import tkinter as tk
from tkinter import filedialog

root=tk.Tk()
root.title('显示图像')
#调整窗口的大小
root.geometry('800x600+300+150')
root['bg']='pink'
path = tk.StringVar()
path1=tk.StringVar()

#设置打开图像的路径
def selectPath():
        path_ = filedialog.askopenfilename()
        path.set(path_)
def selectPath1():
        path_ = filedialog.askopenfilename()
        path1.set(path_)
        
tk.Label(root,text = "单张图片处理路径:").grid(row = 0, column = 0) 
#输入、输出框存放选择路径
tk.Entry(root, textvariable = path, width=60).grid(row = 0, column = 1)
tk.Button(root, text = "单张图片处理选择", command =selectPath).grid(row = 0, column = 2)

tk.Label(root,text = "两图叠加第二路径:").grid(row = 3, column = 0) 
#输入、输出框存放选择路径
tk.Entry(root, textvariable = path1, width=60).grid(row = 3, column = 1)
tk.Button(root, text = "两图叠加第二路径选择", command =selectPath1).grid(row =3, column = 2)


#水平翻转
def shuiping(path_file):
    im=Image.open(path_file)
    a=im.rotate(180)
    a.show()
#自由裁剪
def caijian(path_file):
    box = (780,600,1200,1000)  # 截图的开始、结束坐标
    im=Image.open(path_file)
    region = im.crop(box)
    region.show()
#镜像翻转
def jingxiang(path_file):
    im_copy=Image.open(path_file)
    im_copy.transpose(Image.FLIP_LEFT_RIGHT).show()

#缩略图
def suoluetu(path_file):
     im=Image.open(path_file)
     w,h = im.size
     im.thumbnail((w//12,h//12))
     im.show()
#叠加图
def diejia(path_file,path_file1):
    im_copy=Image.open(path_file)
    im_log = Image.open(path_file1)
    lw,lh = im_log.size                                                            
    im_log_copy = im_log.copy()
    im_log_copy.thumbnail((lw//5,lh//5))    
    im_copy.paste(im_log_copy)
    im_copy.show()                                                                                                                                              
#增强
def zengqiang(path_file):
    img_copy=Image.open(path_file)
    enh = ImageEnhance.Contrast(img_copy)
    enh.enhance(1.5).show()

#灰度图
def rgb_to_gray(path_file):
    bh=Image.open(path_file)  
    bh1= np.array(bh)
    bh2=rgb2gray.img(bh1)
    bh3= Image.fromarray(bh2)
    bh3.show()

#平滑处理
def ph_rh(path_file):
    bh=Image.open(path_file)
    bhh=np.array(bh)
    bh1=rgb2gray.img(bhh)
    window1=gsph.gauss_window(3,1.0)
    img_sobel_mean=gsph.correl2d(bh1,window1)
    #data矩阵转化为二维图片
    new_im = Image.fromarray(img_sobel_mean)
    new_im.show()
#锐化处理图
def rh_cl(path_file):
    bh1=Image.open(path_file)
    bh=np.array(bh1)
    gt=rgb2gray.img(bh)
    lapl=lap.main(gt)
    new_im = Image.fromarray(lapl)
    new_im.show()
#rgb转hsi空间
def rgb_to_hsi(path_file):
    bb=Image.open(path_file)
    bh=np.array(bb)
    hsi_image=np.zeros(bh.shape,dtype='uint8')
    for ii in range(bh.shape[0]):
        for jj in range(bh.shape[1]):
            r,g,b=bh[ii,jj,:]
            h,s,i=rgb2hsi.rgb2hsi(r,g,b)
            hsi_image[ii,jj,:]=(h,s,i)
    im = Image.fromarray(hsi_image)
    im.show()
#傅里叶变换
def fuli(path_file):
    img=Image.open(path_file)
    ji=np.array(img)
    gt=rgb2gray.img(ji)
    a=fuliye.mainn(gt)
    hk=Image.fromarray(a)
    hk.show()
#调整gamma值
def adjust(path_file):    
    img=Image.open(path_file)
    bhj=np.array(img)
    img_1=exposure.adjust_gamma(bhj,25)
    new_im = Image.fromarray(img_1)
    new_im.show()
#颜色分层
def fenceng1(path_file):
    img=Image.open(path_file)
    gh=np.array(img)
    bhj=fenceng.wct(gh)
    #将float转回unit8，要乘以255
    gray = (bhj * 255).astype('uint8')
    bhj1=Image.fromarray(gray)
    bhj1.show()
def origion(path_file):
    im=Image.open(path_file)
    im.show()
#直接选择，不设置按钮绑定    
def CallOn(event):
    if comb.get()=='原图':        
        print(origion(path.get()))
    elif comb.get()=='叠加图':
        print(diejia(path.get(),path1.get()))
    elif comb.get()=='灰度图':
        print(rgb_to_gray(path.get()))
    elif comb.get()=='平滑处理':
       print(ph_rh(path.get()))
    elif comb.get()=='锐化处理':
       print(rh_cl(path.get()))
    elif comb.get()=='RGB转HSI图像':
       print(rgb_to_hsi(path.get()))
    elif comb.get()=='缩略图':
       print(suoluetu(path.get()))
    elif comb.get()=='图像增强':
      print(zengqiang(path.get()))
    elif comb.get()=='调整gamma值':
       print(adjust(path.get()))
    elif comb.get()=='颜色分层':
        print(fenceng1(path.get()))
    elif comb.get()=='滤波处理':  
       print(fuli(path.get()))
    elif comb.get()=='水平翻转':
        print(shuiping(path.get()))
    elif comb.get()=='自由裁剪':
        print(caijian(path.get()))
    elif comb.get()=='镜像翻转':
        print(jingxiang(path.get()))
 

#下拉框按钮设置
#def CallOn(event):
#    if comb.get()=='原图':        
#        tk.Button(root,text='原图',command=origion,bg='blue').grid(row = 3, column = 2)
#    elif comb.get()=='叠加图':
#        tk.Button(root,text='叠加图',command=diejia,bg='pink').grid(row = 3, column = 3)
#    elif comb.get()=='灰度图':
#        tk.Button(root,text='灰度图',command=rgb_to_gray,bg='yellow').grid(row = 3, column = 4)
#    elif comb.get()=='平滑处理':
#        tk.Button(root,text='平滑处理',command=ph_rh,bg='red').grid(row = 3, column = 5)
#    elif comb.get()=='锐化处理':
#        tk.Button(root,text='锐化处理',command=rh_cl,bg='green').grid(row = 3, column = 6)
#    elif comb.get()=='RGB转HSI图像':
#        tk.Button(root,text='rgb2hsi',command=rgb_to_hsi,bg='purple').grid(row = 3, column = 7)
#    elif comb.get()=='缩略图':
#        tk.Button(root,text='缩略图',command=suoluetu,bg='brown').grid(row = 3, column = 8)
#    elif comb.get()=='图像增强':
#        tk.Button(root,text='图像增强',command=zengqiang,bg='with').grid(row =5, column = 3)
#    elif comb.get()=='调整gamma值':
#        tk.Button(root,text='调整garma值',command=adjust,bg='pink').grid(row = 4, column = 3)
#    elif comb.get()=='颜色分层':
#        tk.Button(root,text='颜色分层',command=fenceng1,bg='purple').grid(row = 4, column =4)
#    elif comb.get()=='滤波处理':  
#        tk.Button(root,text='滤波处理',command=fuli,bg='green').grid(row = 4, column = 5)
#    elif comb.get()=='水平翻转':
#         tk.Button(root,text='水平裁剪',command=shuiping,bg='green').grid(row =4, column = 6)
#    elif comb.get()=='自由裁剪':
#         tk.Button(root,text='自由裁剪',command=caijian,bg='pink').grid(row = 4, column = 7)
#    elif comb.get()=='镜像翻转':
#         tk.Button(root,text='镜像翻转',command=jingxiang,bg='blue').grid(row = 4, column = 8)

#固定窗口大小
#root.resizable(False,False)

tk.Label(root,text = "选择图片处理方式 :").grid(row = 6, column = 0) 
var =tk.StringVar()
comb = ttk.Combobox(root,width=60,state='normal',cursor='arrow',
   textvariable=var,values=['原图','灰度图','平滑处理','锐化处理',
       '叠加图','RGB转HSI图像','滤波处理','缩略图','图像增强','调整gamma值',
       '水平翻转','自由裁剪','镜像翻转','颜色分层'],
    )
comb.bind('<<ComboboxSelected>>',CallOn)
comb.grid(row =6, column = 1)

root.mainloop()
